﻿Class Window1
    Friend WithEvents dispatch1 As System.Windows.Threading.Dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher
    Friend WithEvents timer As New System.Windows.Threading.DispatcherTimer(Windows.Threading.DispatcherPriority.Normal, dispatch1)
    Dim v As New bowmanCalc.Class1
    Dim t As New List(Of bowmanCalc.Class1.locations_per_sec)
    Dim i As Integer = 0
    Dim u As Boolean = False
    Dim leftToRight As Boolean = True
    Dim good As System.Media.SoundPlayer = New System.Media.SoundPlayer(My.Resources.tada)
    Dim bound As Double

    Public Sub reset_cannons()
        bound = Rnd() * 100
        cannonbox1.Margin = New Thickness(bound, 0, 0, 0)
        cannonbox2.Margin = New Thickness(0, 0, bound, 0)
    End Sub

    Private Sub fire_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles fire.Click
        t = v.location_times
        arrow.Visibility = Windows.Visibility.Visible
        timer.Interval = TimeSpan.FromSeconds(0.01)
        timer.Start()
    End Sub

    Private Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
        Select Case leftToRight
            Case True
                If i < t.Count Then
                    Me.arrow.Margin = New Thickness(t(i).XLoc + bound, 0, 0, t(i).YLoc)
                    If arrow.Margin.Bottom <= 10 And arrow.Margin.Left >= battleField.Width - bound - 13 And arrow.Margin.Left <= battleField.Width - bound + 13 Then
                        i = 0
                        arrow.Visibility = Windows.Visibility.Hidden
                        good.Play()
                        Dim result As MsgBoxResult = MsgBox("You win!" & vbNewLine & "Play again?", MsgBoxStyle.YesNo)
                        If result = MsgBoxResult.Yes Then
                            leftToRight = True
                            arrow.HorizontalAlignment = Windows.HorizontalAlignment.Left
                            reset_cannons()
                            arrow.Margin = New Thickness(bound, 0, 0, 2.5)
                            timer.Stop()
                        Else
                            End
                        End If
                    End If
                    i += 1
                Else
                    i = 0
                    arrow.Visibility = Windows.Visibility.Hidden
                    leftToRight = False
                    arrow.HorizontalAlignment = Windows.HorizontalAlignment.Right 
                    arrow.Margin = New Thickness(0, 0, bound, 2.5)
                    timer.Stop()
                End If
            Case False
                If i < t.Count Then
                    Me.arrow.Margin = New Thickness(0, 0, t(i).XLoc + bound, t(i).YLoc)
                    If arrow.Margin.Bottom <= 10 And arrow.Margin.Right >= battleField.Width - bound - 13 And arrow.Margin.Right <= battleField.Width - bound + 13 Then
                        i = 0
                        arrow.Visibility = Windows.Visibility.Hidden
                        good.Play()
                        Dim result As MsgBoxResult = MsgBox("You win!" & vbNewLine & "Play again?", MsgBoxStyle.YesNo)
                        If result = MsgBoxResult.Yes Then
                            leftToRight = True
                            arrow.HorizontalAlignment = Windows.HorizontalAlignment.Left
                            reset_cannons()
                            arrow.Margin = New Thickness(bound, 0, 0, 2.5)
                            timer.Stop()
                        Else
                            End
                        End If
                        timer.Stop()
                    End If
                    i += 1
                Else
                    i = 0
                    arrow.Visibility = Windows.Visibility.Hidden
                    leftToRight = True
                    arrow.HorizontalAlignment = Windows.HorizontalAlignment.Left
                    arrow.Margin = New Thickness(bound, 0, 0, 2.5)
                    timer.Stop()
                End If
        End Select
    End Sub

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        good.LoadAsync()
        Randomize()
        reset_cannons()
    End Sub

    Private Sub about_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles about.Click
        Dim form As New About
        form.Show()
    End Sub

    Private Sub angle_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles angle.TextChanged
        If IsNumeric(angle.Text) Then
            v.angle = angle.Text
            cannonPath1.RenderTransform = New RotateTransform(-v.angle)
            cannonPath2.RenderTransform = New RotateTransform(-v.angle)
            u = False
        Else
            If u = False Then
                u = True
                MsgBox("Please enter a number.")
            End If
        End If
    End Sub

    Private Sub speed_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles speed.TextChanged
        If IsNumeric(speed.Text) Then
            v.speed = speed.Text
            u = False
        Else
            If u = False Then
                u = True
                MsgBox("Please enter a number.")
            End If
            End If
    End Sub

    Private Sub info_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles info.Click
        MsgBox("player one is " & CInt(bound).ToString & " feet from the left border." & vbNewLine & "player two is " & CInt(battleField.Width - bound - 8) & " feet from the left edge." & vbNewLine & "the players are " & CInt(battleField.Width - 2 * bound - 16) & " feet away from each other.")
    End Sub
End Class
